package com.bw.unit3.view.activity

import android.content.Intent
import android.text.TextUtils
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.unit3.R
import com.bw.unit3.base.BaseActivity
import com.bw.unit3.databinding.ActivityLoginBinding
import com.bw.unit3.http.HttpUtil
import com.bw.unit3.uitl.DBUtil
import com.bw.unit3.uitl.SpUtil
import com.bw.unit3.viewmodel.LoginViewModel
import kotlinx.coroutines.launch

@Route(path = "/login/LoginActivity")
class LoginFlowActivity :
    BaseActivity<LoginViewModel, ActivityLoginBinding>(R.layout.activity_login) {

    @JvmField
    @Autowired
    var code: Int = 0

    @Autowired
    lateinit var name: String

    override fun initData() {
        viewModel.loginLiveData.observe(this) {
            ToastUtils.showShort(it.message)
            if (it.code == 200) {
                //登录成功，页面跳转
                //把当前登录的用户信息保存到room 数据库
                DBUtil.getShopDb().getLoginDao().insertLogin(it.data)
                //保存token至本地
                SpUtil.putString("token", it.data.token)
//                SPUtils.getInstance().put("token", it.data.token)
            }
        }

        //挂起函数只能被挂起函数调用，或者在协程作用域内容调用
        lifecycleScope.launch {
            viewModel.loginFlowData.collect() {
                ToastUtils.showShort(it.message)
                if (it.code == 200) {
                    //登录成功，页面跳转
                    //把当前登录的用户信息保存到room 数据库
                    DBUtil.getShopDb().getLoginDao().insertLogin(it.data)
                    //保存token至本地
                    SpUtil.putString("token", it.data.token)
//                SPUtils.getInstance().put("token", it.data.token)
                }
            }
        }
    }


    override fun initView() {
        //ARouter参数注入配置
        ARouter.getInstance().inject(this)
        ToastUtils.showShort("dd$code")
        // 从数据库获取 当地登录的用户信息
        val loginBean = DBUtil.shoppingDb.getLoginDao().getLoginBean()
        if (loginBean != null) {
            dataBinding.etName.setText(loginBean.username)
        }

//        dataBinding.etName.setExtractedText()
        //处理登录点击事件
        dataBinding.btn.setOnClickListener {
            if (!HttpUtil.isNet(this@LoginFlowActivity)) {
                ToastUtils.showShort("请开启网络")
                return@setOnClickListener
            }

            val name = dataBinding.etName.text.toString()
            val pass = dataBinding.etPass.text.toString()
            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass)) {
                ToastUtils.showShort("用户名或密码不能为空")
                return@setOnClickListener
            }

            val map = HashMap<String, String>()
            map["username"] = name
            map["password"] = pass

            //使用livedata更新数据
            //viewModel.login(map)


            //使用flow 更新数据
            viewModel.loginFlow(map)


        }

        dataBinding.tvRegister.setOnClickListener {

            startActivity(Intent(this@LoginFlowActivity, RegisterActivity::class.java))
        }
    }
}